<?php
/**
 * Created by PhpStorm.
 * User: yangyulu
 * Date: 2020/3/5
 * Time: 下午6:04
 */
class Admin_materialController extends Admin_baseController{
    public function listAction(){
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);

        $title = Request::getParams('name');

        $where = $value = $url = array();
        if ($title) {
            $where[] = 'name like ?';
            $value[] = '%' . $title . '%';
            $url['name'] = $title;
        }
        $where[]="type=?";
        $value[]=1;
        $total = self::model('project_material')->count(null, $where, $value);
        $list = self::model('project_material')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);

        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'total' => $total,
            'list' => $list,
            'name' => $title,
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_material/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
        ));
        $this->display();
    }

    public function fileeditAction(){
        $ids=Request::getParams("ids");
        $data['name'] = Request::getParams('name');
        $data['path'] = Request::getParams('path');
        $data['project_id']=Request::getParams('project_id');
        if($ids&&!$info = self::model('project_material')->getOne('id=?', $ids)){
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if(Request::isGet()){
            $this->assign([
                'info'=>$info,
                'categoryList' => self::model('project_list')->getList(),
            ]);

            $this->display();
            exit();
        }

        if(empty($data['path'] )){
            Response::showMsg('请上传文件');
        }
        if(empty($data['name'] )){
            Response::showMsg('请输入资料名');
        }
        if(empty($data['project_id'] )){
            Response::showMsg('请选择项目');
        }
        $data["type"]=1;
        $data["update_time"]=time();
        if($ids){
            self::model("project_material")->update($data,"id=?",$ids);
            Response::ajax(200, '修改成功');
        }else{
            $data["add_time"]=time();
            self::model("project_material")->insert($data);
            Response::ajax(200, '添加成功');
        }
    }
}